/*
 * VelocityRenderedResource.java
 *
 * Copyright 2013 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.springframework.core.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeSingleton;
import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.springframework.core.io.Resource;

/**
 * The result of applying a Apache Velocity <tt>template</tt> to a
 * <tt>model</tt> as a {@link Resource}.
 *
 * @author Jack Leow
 * @since May 2013
 */
public class VelocityRenderedResource
        extends AbstractTemplateRenderedResource<Template> {
    public VelocityRenderedResource(
            Resource template, Map<String,?> model) {
        super(template, model);
    }
    
    public VelocityRenderedResource(
            Resource template, Charset charset, Map<String,?> model) {
        super(template, charset, model);
    }
    
    private static final RuntimeServices runtimeServices =
        RuntimeSingleton.getRuntimeServices();
    
    @Override
    protected Template createTemplate(
            InputStream templateResourceInputStream, Charset charset)
            throws IOException {
        final Reader tmplReader =
            new InputStreamReader(templateResourceInputStream, charset);
        try {
            final SimpleNode node =
                runtimeServices.parse(tmplReader, "spring-templating");
            Template template = new Template();
            template.setRuntimeServices(runtimeServices);
            template.setData(node);
            template.initDocument();
            
            return template;
        } catch (ParseException e) {
            throw new ParseErrorException(e, getDescription());
        }
    }

    @Override
    protected InputStream applyTemplate(
            Template template, Charset charset, Map<String, ?> model)
            throws IOException {
        // Create model
        VelocityContext context = new VelocityContext(model);
        
        // Apply template
        StringWriter content = new StringWriter();
        template.merge(context, content);
        content.close();
        
        return new ByteArrayInputStream(
            content.getBuffer().toString().getBytes(charset)
        );
    }
}
